﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using vngoodlife.Singleton;
using System.Web.Security;

namespace vngoodlife.Util
{
    public class CheckMemberVisibilityAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string userName = HttpContext.Current.User.Identity.Name;
            
                if (userName != "" && DataSingleton.Instance.GetLinQContext().Members.SingleOrDefault(u => u.MemberName == userName).IsActive)
                {
                    return;
                }
                else
                {
                    FormsAuthentication.SignOut();
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{
                                       {"controller", "Home"},{"action", "Index"}
                                       });
                }
            
            //   throw new UnauthorizedAccessException("The record does not exist or you do not have permission to access it");
            //filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{
            //                           {"controller", "Error"},{"action", "NotAuthorized"}
            //                           });
        }
    }
}